package cn.mw.cmdb.processor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.entity.InstanceInfoExtend;
import cn.mw.cmdb.entity.UserGroupTreeView;
import cn.mw.cmdb.param.InstanceSearchParam;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.InstanceServiceImpl;
import cn.mw.cmdb.util.AssetsTool;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.cmdb.view.ViewConvertContext;
import cn.mw.microMonitorCommon.api.cmdb.TreeView;
import cn.mw.microMonitorCommon.api.user.MwUserApi;
import cn.mw.microMonitorCommon.entity.userDTO.GroupDTO;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;

import static cn.mw.cmdb.util.ValConvertUtil.intValueConvert;
import static cn.mw.cmdb.util.ValConvertUtil.longValueConvert;

public class TreeProcessorUserGroup implements TreeProccessorTable {
    private CmdbServiceManage cmdbServiceManage;

    public TreeProcessorUserGroup(CmdbServiceManage cmdbServiceManage){
        this.cmdbServiceManage = cmdbServiceManage;
    }

    @Override
    public List genTreeData(TableDataProcessorContext context) throws Exception {
        List<TreeView> ret = new ArrayList<>();
        MwUserApi mwUserApi = (MwUserApi)context.get(ViewConvertContext.MwuserApiKey);
        InstanceServiceImpl instanceService = (InstanceServiceImpl) cmdbServiceManage.getModelService(InstanceServiceImpl.ID);

        List<String> fields = new ArrayList<>();
        fields.add(CMDBTool.getFieldName(InstanceInfo::getId));
        fields.add(CMDBTool.getFieldName(InstanceInfo::getInstanceName));
        fields.add(CMDBTool.getFieldName(InstanceInfo::getGroups));
        fields.add(CMDBTool.getFieldName(InstanceInfo::getItemAssetsStatus));
        InstanceSearchParam instanceSearchParam = new InstanceSearchParam();
        instanceSearchParam.setFields(fields);
        BeanUtil.copyProperties(context.getTableSearchParam(), instanceSearchParam, CopyOptions.create().setIgnoreNullValue(true));

        List<InstanceInfoExtend> list = instanceService.aggregateList(instanceSearchParam);
        Set<Long> groupIdSet = new HashSet<>();
        Map<Long, List<InstanceInfoExtend>> id2InstancesMap = new HashMap<>();
        if (null != list) {
            for (InstanceInfoExtend instanceInfo : list) {
                if (CollectionUtils.isNotEmpty(instanceInfo.getGroups())) {
                    groupIdSet.addAll(instanceInfo.getGroups());
                    instanceInfo.getGroups().forEach(item -> {
                        if (!id2InstancesMap.containsKey(item)) {
                            id2InstancesMap.put(item, new ArrayList<>());
                        }
                        id2InstancesMap.get(item).add(instanceInfo);
                    });
                }
            }
        }

        if(groupIdSet.size() > 0){
            List<GroupDTO> groupDTOS = mwUserApi.getGroupListByIds(groupIdSet.stream().toList());
            if(null != groupDTOS){
                for(GroupDTO groupDTO: groupDTOS){
                    UserGroupTreeView userGroupTreeView = new UserGroupTreeView(groupDTO.getId().toString()
                            ,groupDTO.getGroupName(), null);
                    ret.add(userGroupTreeView);
                }
            }
        }
        for(TreeView treeView : ret) {
            long id = longValueConvert(treeView.getId());
            if (id2InstancesMap.containsKey(id)) {
                treeView.setCount(intValueConvert(id2InstancesMap.get(id).size()));
                treeView.setItemAssetsStatus(AssetsTool.calcItemAssetsStatus(id2InstancesMap.get(id), treeView));
            }
        }
        return ret;
    }
}
